package com.travel.photos.ui.find.search;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.travel.photos.bean.FindSearchBean;
import com.travel.photos.bean.FindSearchResBean;
import com.travel.photos.config.UrlConfig;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;


/**
 * Created by Administrator on 2016/10/17.
 */
public class PhotoSearchUserPresenter implements PhotoSearchUserContract.Presenter {
//逻辑处理
    private PhotoSearchUserContract.View view;
    private PhotoSearchUserContract.Model model;
    private String searchcontent;

    public PhotoSearchUserPresenter(PhotoSearchUserContract.View view,String searchcontent) {
        this.view = view;
        this.model = new PhotoSearchUserModel();
        try {
            this.searchcontent = URLDecoder.decode(searchcontent,"UTF-8");
            Log.i("tag", "PhotoSearchUserPresenter: "+this.searchcontent);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private int page=1;
    private String nextpage="1";
    private Map<String,String> map = new HashMap<>();
    private List<FindSearchResBean> traveresbeans;
    private SearchUserAdapter adapter;

    @Override
    public void getDetail(final Context context) {
        map.put(UrlConfig.Params.PARAMTRPE,"get");
        map.put(UrlConfig.Params.CONDITION,searchcontent);
        map.put(UrlConfig.Params.PAGE,page+"");

        model.getDetail(map, new Subscriber<FindSearchBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(nextpage==""){
                    Toast.makeText(context,"数据加载完了",Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(context,"网络错误,小逗推荐换个wifi试试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNext(FindSearchBean travelDetail) {
                if (page==1){
                    traveresbeans = travelDetail.getData().getRes();
                    nextpage = travelDetail.getData().getNextPage();
                    adapter= new SearchUserAdapter(context,traveresbeans);
                    view.getDetail(adapter);
                }else {
                    traveresbeans.addAll(travelDetail.getData().getRes());
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }
    public void UpRefresh(Context context){
        page++;
        getDetail(context);
    }
}
